<?php
/**
 * @author: liaofei<136327134@qq.com>
 * @day: 2021/6/8
 */

namespace App\Dao\user;

use App\Dao\BaseDao;
use App\Models\system\SystemAdminModel;
use App\Models\user\UserAgentModel;
use Illuminate\Support\Facades\DB;
use App\Dao\system\SystemAdminDao;
use App\Exceptions\ApiException as HOException;
use App\Services\system\SystemLogServices;

/**
 * Class UserInfoDao
 * @package app\dao\user
 */
class UserAgentDao extends BaseDao
{

    public function setModel(){
        return UserAgentModel::class;
    }


    /**
     * 代理商列表
     * @param string $account
     */
    public function AgentList(array $where,$post)
    {
       $arr =  $this->formatData(SystemAdminModel::where($where)->where('level',2)->paginate($post['limit']));

       return $arr;
    }

    /**
     * 创建代理商
     */
    public  function  AgentSave(array $arr){

        DB::beginTransaction();
        try{

            $systemAdminServices = app()->make(SystemAdminDao::class);
            $systemAdminServices->create($arr);
            // $agent['admin_uid']=$admin_id;
            //$this->insert($agent);
            //插入日志
            $LogServices = app()->make(SystemLogServices::class);
            $LogServices->SaveLog($arr['creator_uid'],'添加代理商','代理商账号:'.$arr['account'].'--'.'代理商名称:'.$arr['real_name']);

            DB::commit();
            return true;
        } catch(\Exception $exception) {
            DB::rollBack();
            throw new HOException($exception->getMessage());

        }
    }


    /**
     * 修改代理商
     */
    public  function  AgentUpdate(string $agent_id,$arr){
        unset($arr['agent_id']);
        DB::beginTransaction();
        try{

            $systemAdminServices = app()->make(SystemAdminDao::class);

            /* $creator_uid=$user['creator_uid'];
             unset($user['creator_uid']);*/
            $admin_id= $systemAdminServices->update(['id'=>$agent_id],$arr);

            // $this->update(['id'=>$agent_id],$agent);
            //插入日志
            $LogServices = app()->make(SystemLogServices::class);
            $LogServices->SaveLog($agent_id,'修改代理商','代理商id:'.$agent_id.'代理商账号:'.$arr['account'].'--'.'代理商名称:'.$arr['real_name']);

            DB::commit();
            return true;
        } catch(\Exception $exception) {
            DB::rollBack();
            throw new HOException($exception->getMessage());

        }
    }

    /**
     * 获取代理信息
     */
    public  function  getAgent(array $where){
        $res = SystemAdminModel::where($where)->first();
        $data =  $this->formatData($res);
        unset($data['pwd']);
        return $data;
    }

    /**
     * 软删除代理商
     */
    public  function  agentDelte($admin_id,$agent_id){
        DB::beginTransaction();
        try{

            $systemAdminServices = app()->make(SystemAdminDao::class);
            $systemAdminServices->update(['id'=>$agent_id],['deleted_at'=>date('Y-m-d H:i:s')]);

            //$this->update(['id'=>$agent_id],['deleted_at'=>date('Y-m-d H:i:s')]);
            //插入日志
            $LogServices = app()->make(SystemLogServices::class);
            $LogServices->SaveLog($admin_id,'删除代理商','代理商id:'.$agent_id);

            DB::commit();
            return true;
        } catch(\Exception $exception) {
            DB::rollBack();
            throw new HOException($exception->getMessage());

        }
    }

}

